# rocky 9.1 ignore the fallthrough warnings
rocky9_1=5.14.0-162.6.1.el9_1.x86_64
# rocky 9.2
rocky9_2=5.14.0-284.11.1.el9_2.x86_64
# Kylin
kylin_4_19_90_89=4.19.90-89.11.v2401.ky10.x86_64
kylin_4_19_90_52=4.19.90-52.22.v2207.ky10.aarch64
kylin_4_19_90_52_15=4.19.90-52.15.v2207.ky10.x86_64
kylin_4_19_90_52_22_x86=4.19.90-52.22.v2207.ky10.x86_64
# rhel 8.6 or rocky 8.6 have the same kernel
rhel8_6=4.18.0-372.9.1.el8.x86_64
ubuntu22_04=5.15.0-25-generic
# rhel9.0_x86
rhel9_0_x86=5.14.0-70.22.1.el9_0.x86_64
# rhel8.4_x86
rhel8_4_x86=4.18.0-305.el8.x86_64
# uos_1070e
uos_1070e=4.19.90-2403.3.0.0270.84.uel20.x86_64
#UnionTech OS Server 20 1050u2e
uos_20_1050u2e=4.19.90-2211.5.0.0178.22.uel20.aarch64

ifeq ($(rocky9_1), $(shell uname -r))
EXTRA_CFLAGS += -DROCKY9_VERSION
else ifeq ($(rocky9_2), $(shell uname -r))
EXTRA_CFLAGS += -DROCKY9_VERSION
EXTRA_CFLAGS += -DROCKY92
else ifeq ($(kylin_4_19_90_89), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(kylin_4_19_90_52), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(kylin_4_19_90_52_15), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(kylin_4_19_90_52_22_x86), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
else ifeq ($(rhel8_6), $(shell uname -r))
EXTRA_CFLAGS += -DREDHAT8_6
else ifeq ($(ubuntu22_04), $(shell uname -r))
EXTRA_CFLAGS += -DUBUNTU22_04
else ifeq ($(shell uname -r), $(rhel9_0_x86))
EXTRA_CFLAGS += -DROCKY9_VERSION
EXTRA_CFLAGS += -DRHEL9
else ifeq ($(rhel8_4_x86), $(shell uname -r))
EXTRA_CFLAGS += -DREDHAT8_6
EXTRA_CFLAGS += -DREDHAT8_4
else ifeq ($(uos_1070e), $(shell uname -r))
EXTRA_CFLAGS += -DKYLIN_4_19
EXTRA_CFLAGS += -DUOS_1070E
else ifeq ($(uos_20_1050u2e), $(shell uname -r))
EXTRA_CFLAGS += -DUOS_4_19_90
endif

EXPORT_SYMBOL := true

OFED_VERSION := OFED_MLNX_5_7_8
TOP_DIR := $(PWD)/../../../

KBUILD_EXTRA_SYMBOLS += $(TOP_DIR)/output/UDK_Module.symvers
KBUILD_EXTRA_SYMBOLS += $(TOP_DIR)/output/SDK_Module.symvers
KBUILD_EXTRA_SYMBOLS += $(TOP_DIR)/output/NIC_Module.symvers

EXTRA_CFLAGS += -DHW_CONVERT_ENDIAN
EXTRA_CFLAGS += -DROCE_SERVICE
EXTRA_CFLAGS += -D__ROCE_DFX__
EXTRA_CFLAGS += -DROCE_CC_EN
#EXTRA_CFLAGS += $(HI1823_FLAGS)
EXTRA_CFLAGS += -Werror -Wall
EXTRA_CFLAGS += -DROCE_STANDARD
EXTRA_CFLAGS += -DHIUDK_SDK -DHIUDK_ULD -DHAVE_KS_SUPPORT

EXTRA_CFLAGS += -Werror -Wno-implicit-fallthrough

EXTRA_CFLAGS += -fstack-protector-all

ORIGN_LINUXINCLUDE := $(LINUXINCLUDE)

KBUILD_EXTRA_SYMBOLS+=/usr/src/ofa_kernel/default/Module.symvers
THIRDPARTY_INC = /usr/src/ofa_kernel/default/
EXTRA_CFLAGS += -D$(OFED_VERSION)
LINUXINCLUDE := -I$(THIRDPARTY_INC)/include/ -I$(THIRDPARTY_INC)/include/uapi $(ORIGN_LINUXINCLUDE)

EXTRA_CFLAGS += -DROCE_COMPUTE
EXTRA_CFLAGS += -DROCE_BONDING_EN
EXTRA_CFLAGS += -DROCE_STANDARD

# Set CFLAGS from default file directories
EXTRA_CFLAGS += -I$(TOP_DIR)/include/rdma
EXTRA_CFLAGS += -I$(TOP_DIR)/include
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/cq
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/dfx
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/include
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/mr
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/qp
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/rdma
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/tc
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/srq
EXTRA_CFLAGS += -I$(TOP_DIR)/roce/linux/kernel/bond
EXTRA_CFLAGS += -I$(TOP_DIR)/hinic/bond
EXTRA_CFLAGS += -I$(TOP_DIR)/host/hmm
EXTRA_CFLAGS += -I$(TOP_DIR)/hwsdk/crm
EXTRA_CFLAGS += -I$(TOP_DIR)/hwsdk/crm/lld
EXTRA_CFLAGS += -I$(TOP_DIR)/hwsdk/hwif
EXTRA_CFLAGS += -I$(TOP_DIR)/host/include
EXTRA_CFLAGS += -I$(TOP_DIR)/include/public
EXTRA_CFLAGS += -I$(TOP_DIR)/include/nic
EXTRA_CFLAGS += -I$(TOP_DIR)/include/mag
EXTRA_CFLAGS += -I$(TOP_DIR)/include/mpu
EXTRA_CFLAGS += -I$(TOP_DIR)/include/cfg_mgmt
EXTRA_CFLAGS += -I$(TOP_DIR)/include/bond
EXTRA_CFLAGS += -I$(TOP_DIR)/include/cqm
EXTRA_CFLAGS += -I$(TOP_DIR)/include/dfx
EXTRA_CFLAGS += -I$(TOP_DIR)/include/hw
EXTRA_CFLAGS += -I$(TOP_DIR)/platform/huawei_secure_c/include
EXTRA_CFLAGS += -I$(TOP_DIR)/platform/huawei_secure_c/src
# #OSSL

# #UDK
EXTRA_CFLAGS += -I$(TOP_DIR)/udk/include

export KLIB_BUILD
export KERN_VERSION
export MAKE
export KBUILD_EXTRA_SYMBOLS

$(info ============== INFO ==============)
$(info Building kernel modules)
$(info Driver Version: $(VERSION))
$(info Kernel Version: $(KERN_VERSION))
$(info os type: $(HI1823_OS_TYPE))
$(info Kernel Sources: $(KSRC))
$(info Thirdparty Source: $(THIRDPARTY_INC))
$(info linux include: $(LINUXINCLUDE))
$(info Thirdparty Symbols: $(KBUILD_EXTRA_SYMBOLS))
$(info build_dir: $(HI1823_BUILD_DIR))
$(info ==================================)
$(info linux version $(LINUX_VERSION_CODE))
$(info $(EXTRA_CFLAGS))
$(info ==================================)

obj-m += hiroce3.o

hiroce3-y :=../../../platform/huawei_secure_c/src/securecutil.o \
                        ../../../platform/huawei_secure_c/src/secureinput_a.o \
                        ../../../platform/huawei_secure_c/src/secureprintoutput_a.o \
                        ../../../platform/huawei_secure_c/src/memset_s.o \
                        ../../../platform/huawei_secure_c/src/memcpy_s.o \
                        ../../../platform/huawei_secure_c/src/strncpy_s.o \
                        ../../../platform/huawei_secure_c/src/strncat_s.o \
                        ../../../platform/huawei_secure_c/src/sprintf_s.o \
                        ../../../platform/huawei_secure_c/src/vsprintf_s.o \
                        ../../../platform/huawei_secure_c/src/vsnprintf_s.o \
                        ../../../platform/huawei_secure_c/src/snprintf_s.o \
                        ../../../platform/huawei_secure_c/src/vsscanf_s.o \
                        ../../../platform/huawei_secure_c/src/sscanf_s.o \
                        ../../../platform/huawei_secure_c/src/memmove_s.o \
                        ../../../platform/huawei_secure_c/src/strcpy_s.o \
                        ../../../platform/huawei_secure_c/src/strcat_s.o \
                        ../../../platform/huawei_secure_c/src/strtok_s.o \
                        cq/roce_cq_common.o \
                        cq/roce_cq_cqe.o \
                        cq/roce_cq_create.o \
                        cq/roce_cq_ctrl.o \
                        cq/roce_cq_destroy.o \
                        tc/roce_tc.o \
                        extension/roce_event_extension.o \
                        extension/roce_main_extension.o \
                        extension/roce_mr_extension.o \
                        extension/roce_netdev_extension.o \
                        extension/roce_qp_extension.o \
                        extension/roce_qp_post_send_extension.o \
                        extension/roce_srq_extension.o \
                        extension/roce_cdev_extension.o \
                        roce_db.o \
                        roce_main.o \
                        roce_netlink.o \
                        roce_event.o \
                        roce_netdev.o \
                        roce_mix.o \
                        mr/roce_mr.o \
                        roce_pd.o \
                        qp/roce_qp_create.o \
                        qp/roce_qp_destroy.o \
                        qp/roce_qp_modify.o \
                        qp/roce_qp_post_recv.o \
                        qp/roce_qp_post_send.o \
                        qp/roce_qp_query.o \
                        roce_xrc.o \
                        dfx/roce_dfx.o \
                        dfx/roce_dfx_query.o \
                        roce_cdev.o \
                        roce_sysfs.o \
                        roce_cmd.o \
                        roce_cqm_cmd.o \
                        bond/roce_bond_common.o \
                        bond/roce_bond_register.o \
                        bond/roce_bond_unregister.o \
                        rdma/rdma_bitmap.o \
                        rdma/rdma_comp.o \
                        rdma/rdma_comp_res.o \
                        rdma/rdma_comp_gid.o \
                        rdma/rdma_comp_init.o \
                        rdma/rdma_comp_pd.o \
                        rdma/rdma_comp_mw_mr.o \
                        srq/roce_srq_comm.o \
                        srq/roce_srq_create.o \
                        srq/roce_srq_ctrl.o \
                        ../../../host/hmm/hmm_buddy.o \
                        ../../../host/hmm/hmm_comp.o \
                        ../../../host/hmm/hmm_comp_init.o \
                        ../../../host/hmm/hmm_comp_mtt.o \
                        ../../../host/hmm/hmm_comp_mw_mr.o \
                        ../../../host/hmm/hmm_comp_res.o \
                        ../../../host/hmm/hmm_em.o \
                        ../../../host/hmm/hmm_mr.o \
                        ../../../host/hmm/hmm_umem.o

hiroce3-y +=dfx/roce_dfx_cap.o


SYS_TIME=$(shell date +%Y-%m-%d_%H:%M:%S)
ccflags-y += -D __TIME_STR__=\"$(SYS_TIME)\"
GLOBAL_VERSION=$(shell cat $(TOP_DIR)/GLOBAL_VERSION_NEW | grep driver | awk -F ':' '{print $$2}')
ccflags-y += -DGLOBAL_VERSION_STR=\"$(GLOBAL_VERSION)\"

$(warning cflags, $(ccflags-y))
V ?= 0

ccflags-y += -DHW_CONVERT_ENDIAN

ccflags-y += -D__LINUX__

EXTRA_CFLAGS += -DSECUREC_EXPORT_KERNEL_SYMBOL=0

KERNEL_VER := $(shell uname -r 2>null)

KERNEL_DIR := /lib/modules/$(KERNEL_VER)/build

default:
	$(MAKE) -C $(KERNEL_DIR) M=$(shell pwd) -W modules

clean:
	rm -rf *.o *.ko *.order .*.cmd *.mod.* .H* .tm* .tmp_versions Module.symvers *.ko.unsigned null
